<script lang="ts" setup>
import { ref,getCurrentInstance } from 'vue'
// 导入api方法
import {userEdit} from '@/api/user'

//获取当前Vue实例
const instance = getCurrentInstance()

//弹框开关变量
const dialogVisible = ref(false)
//方法定义
const show=(val)=>{
  dialogVisible.value=val
}

// 表单对象
const userForm=ref({
  id:null,
  name:null,
  gender:null,
  createTime:null
})

//提交表单，修改用户信息
const editSubmit=async()=>{
  //远程调用
  let result=await userEdit(userForm.value)
  console.log(result)
  //关窗
  dialogVisible.value=false

  //向父组件传递事件
  //vue2 this代表当前Vue实例对象,Vue3不支持
  instance.emit('search')
}

// 默认大小
const size='default'

//向父组件暴露变量或者方法
defineExpose({
  //暴露变量
  dialogVisible,
  userForm,
  //暴露方法
  show
})
</script>
<template>
  <el-dialog
    v-model="dialogVisible"
    title="Tips"
    width="500"
  >
    <el-form :model="userForm" label-width="auto" style="max-width: 600px">
      <el-form-item label="姓名">
        <el-input v-model="userForm.name" />
      </el-form-item>
      <el-form-item label="性别">
        <el-select v-model="userForm.gender" placeholder="请选择性别">
          <el-option label="男" value="1" />
          <el-option label="女" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="userForm.createTime"
          type="date"
          placeholder="请选择日期"
          :size="size"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="editSubmit">提交</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

